import { defineStore } from "pinia";
import { computed, ref } from "vue";
import { useUserStore } from "./user";
import { addCartAPI, delCartAPI, findNewCartListAPI, mergeCartAPI } from "@/apis/cart"

export interface CartItem {
	skuId: string,
	count: number,
	price: number,
	selected: boolean,
}

export const useCartStore = defineStore("cart", () => {
	const userStore = useUserStore();
	const cartList = ref([]);
	const isAllSelected = ref(false);

	const isLogin = computed(() => {
		return !!userStore.userInfo?.token;
	});
	async function updateNewList() {
		const res: any = await findNewCartListAPI();
		cartList.value = res.result;
	}
	async function addCart(goods: CartItem) {
		console.log("isLogin = " + isLogin.value);
		const { skuId, count } = goods;
		if (!isLogin.value) {
			for (let item of cartList.value as CartItem[]) {
				if (item.skuId === goods.skuId) {
					item.count++;
					return;
				}
			}
			cartList.value.push(goods as never);
		}
		else {
			await addCartAPI({ skuId, count });
			updateNewList();
		}
	}
	async function delCart(skuId: string) {
		if (!isLogin.value) {
			for (let i = 0; i < cartList.value.length; i++) {
				const item: CartItem = cartList.value[i];
				if (item.skuId === skuId) {
					cartList.value.splice(i, 1);
					return;
				}
			}
		}
		else {
			await delCartAPI([skuId]);
			updateNewList();
		}
	}
	function singleCheck(skuId: string) {
		for (let item of cartList.value as CartItem[]) {
			if (item.skuId === skuId) {
				item.selected = item.selected ? false : true;
				if (!item.selected) {
					isAllSelected.value = false;
				}
			}
		}
	}
	function allCheck() {
		isAllSelected.value = isAllSelected.value ? false : true;
		for (let item of cartList.value as CartItem[]) {
			item.selected = isAllSelected.value;
		}
	}
	function clearCart() {
		cartList.value = [];
	}
	async function mergeCart() {
		await mergeCartAPI((cartList.value as CartItem[]).map(item => {
			return {
				skuId: item.skuId,
				count: item.count,
				selected: item.selected,
			}
		}));
		updateNewList();
	}

	const allCount = computed(() => {
		let allCount = 0;
		for (let item of cartList.value as CartItem[]) {
			allCount += item.count;
		}
		return allCount;
	});
	const allPrice = computed(() => {
		let allPrice = 0;
		for (let item of cartList.value as CartItem[]) {
			allPrice += item.count * item.price;
		}
		return allPrice;
	});
	const allSelected = computed(() => {
		let res = 0;
		for (let item of cartList.value as CartItem[]) {
			if (item.selected) res += item.count;
		}
		return res;
	});
	const allSelectedPrice = computed(() => {
		let res = 0;
		for (let item of cartList.value as CartItem[]) {
			if (item.selected) res += item.count * item.price;
		}
		return res;
	});
	// const isAllSelected = computed(() => {
	// 	return allSelected.value === cartList.value.length;
	// });

	return { cartList, addCart, delCart, singleCheck, allCheck, clearCart, mergeCart, allCount, allPrice, allSelected, allSelectedPrice, isAllSelected };
}, {
	persist: true,
});